package com.dutiantech.partfastdfs.service;

import com.dutiantech.partfastdfs.bean.FastdfsStorgeResult;
import com.dutiantech.partfastdfs.config.ConfigUtil;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class FileProcessService {
	
	private final static Logger logger = LoggerFactory.getLogger(FileProcessService.class);


    @Autowired
    private ConfigUtil configUtil;
    /**
     * 文件上传
     * @param multipartFile  A representation of an uploaded file received in a multipart request
     * @param file_ext_name 扩展名
     * @return
     * @throws Exception
     */
    public  FastdfsStorgeResult imgUpload(MultipartFile multipartFile, String file_ext_name,String  sys) throws Exception {
    	
    	logger.info(String.format("文件上传：file_ext_name=%s",file_ext_name));
    	
        byte[] fileBuffer = multipartFile.getBytes();
        return init().upFileByBytes(multipartFile.getSize(),fileBuffer,file_ext_name,sys);
    }
    public  FastdfsStorgeResult fileUpload(MultipartFile multipartFile, String file_ext_name,String sys) throws Exception{
    	
    	logger.info(String.format("文件上传：file_ext_name=%s",file_ext_name));
    	
       byte[] fileBuffer = multipartFile.getBytes();
       return init().upFileByBytes(multipartFile.getSize(),fileBuffer,file_ext_name,sys);
    }

    public  FastdfsStorgeResult fileUpload( byte[] fileBuffer,long multipartFileSize, String file_ext_name,String sys) throws Exception{
    	
    	logger.info(String.format("文件上传：file_ext_name=%s",file_ext_name));
    	
        return init().upFileByBytes(multipartFileSize,fileBuffer,file_ext_name,sys);
    }
    
    public InputStream downFile(String path) throws Exception {
    	
    	logger.info(String.format("文件下载：path=%s",path));
    	
    	byte[] fileBytes = init().downImg(path);
    	
    	InputStream inputStream = new ByteArrayInputStream(fileBytes);


    	
    	return inputStream;
    }

    public byte[] downFileByte(String path) throws Exception{

        logger.info(String.format("文件下载：path=%s",path));
        byte[] fileBytes = init().downImg(path);
        return fileBytes;
    }
    ImgProcessUtil init(){
        return new ImgProcessUtil(configUtil);
    }
}
